package org.example.chatroom.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.chatroom.common.pojo.dataobject.Friend;
import org.example.chatroom.common.pojo.dataobject.FriendRequest;
import org.example.chatroom.common.pojo.dataobject.UserInfo;
import org.example.chatroom.common.pojo.response.RequestMessageResponse;
import org.example.chatroom.mapper.FriendMapper;
import org.example.chatroom.mapper.FriendRequestMapper;
import org.example.chatroom.mapper.UserMapper;
import org.example.chatroom.service.FriendRequestService;
import org.example.chatroom.service.FriendService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LIUYANJIAO
 * Date: 2025-07-20
 * Time: 17:24
 */
@Service
public class FriendRequestServiceImpl implements FriendRequestService {
    @Autowired
    private FriendRequestMapper friendRequestMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private FriendMapper friendMapper;

    @Override
    public List<RequestMessageResponse> getRequestMessage(Integer userId) {
        QueryWrapper<FriendRequest> friendRequestQueryWrapper = new QueryWrapper<>();
        friendRequestQueryWrapper.lambda().eq(FriendRequest::getToUserId,userId);
        List<FriendRequest> friendRequests = friendRequestMapper.selectList(friendRequestQueryWrapper);
        List<Integer> userIds = friendRequests.stream()
                .map(FriendRequest::getFromUserId)
                .distinct()
                .collect(Collectors.toList());
        //查询user详情
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.lambda().in(UserInfo::getId,userIds);
        List<UserInfo> userInfos = userMapper.selectList(userInfoQueryWrapper);
        Map<Integer,UserInfo> userInfoMap = userInfos.stream()
                .collect(Collectors.toMap(UserInfo::getId,d -> d));
        //处理每一个请求
        List<RequestMessageResponse> responses = friendRequests.stream().map(friendRequest -> {
            RequestMessageResponse response = new RequestMessageResponse();
            BeanUtils.copyProperties(friendRequest,response);
            response.setFriendRequestId(friendRequest.getId());
            response.setUserName(userInfoMap.get(friendRequest.getFromUserId()).getUserName());
            response.setAvatarUrl(userInfoMap.get(friendRequest.getFromUserId()).getAvatarUrl());
            return response;
        }).collect(Collectors.toList());
        return responses;

    }

    @Override
    public boolean handleRequest(boolean resp, Integer friendRequestId) {
        if(resp){
            //同意——改变状态，添加好友
            FriendRequest friendRequest = new FriendRequest();
            friendRequest.setStatus("accept");
            friendRequest.setId(friendRequestId);
            friendRequestMapper.updateById(friendRequest);

            FriendRequest friendRequest1 = friendRequestMapper.selectById(friendRequestId);
            Friend friend = new Friend();
            friend.setUserId(friendRequest1.getFromUserId());
            friend.setFriendId(friendRequest1.getToUserId());

            Friend friend1 = new Friend();
            friend1.setUserId(friendRequest1.getToUserId());
            friend1.setFriendId(friendRequest1.getFromUserId());

            friendMapper.insert(friend1);
            friendMapper.insert(friend);
            return true;
        }
        else {
            //拒绝好友请求
            FriendRequest friendRequest = new FriendRequest();
            friendRequest.setStatus("reject");
            friendRequest.setId(friendRequestId);
            friendRequestMapper.updateById(friendRequest);
            return false;
        }
    }
}
